'''
Created on May 10, 2011

@author: bruk
'''

import struct #@UnresolvedImport
import pygame #@UnresolvedImport
import pygame.locals #@UnresolvedImport

class Map:
    def __init__(self,mapfilename):
        mapFile = open(mapfilename,'r')
        
        self.size = [struct.unpack('i', mapFile.read(4)), struct.unpack('i', mapFile.read(4)) ]
        
        self.tilesetFileNameLength = struct.unpack('i', mapFile.read(4))
        self.tilesetFileName = struct.unpack('s' + self.tilesetFileNameLength, mapFile.read(self.tilesetFileNameLength)) 
        
        self.tileSize = [struct.unpack('i', mapFile.read(4)), struct.unpack('i', mapFile.read(4)) ]
        
        ## load tile images
        image = pygame.image.load(self.tilesetFileName).convert()
        image_width, image_height = image.get_size()
        self.tileset = []
        for tile_x in range(0, image_width/self.tileSize[0]):
            line = []
            for tile_y in range(0, image_height/self.tileSize[1]):
                rect = (tile_x*self.tileSize[0], tile_y*self.tileSize[1], self.tileSize[0], self.tileSize[1])
                line.append(image.subsurface(rect))
                self.tileset.append(line)

